import Vue from "vue";
import Router from "vue-router";

Vue.use(Router);

const routes = [
  {
    name: "",
    path: "/",
    component: () => import("../page/index"),
    meta: {
      title: "首页"
    }
  },
  {
    name: "detail",
    path: "/detail",
    component: () => import("../page/home/detail"),
    meta: {
      title: "详情"
    }
  },
  {
    name: "login",
    component: () => import("../page/account/login"),
    meta: {
      title: "登录"
    }
  },
  {
    name: "zfbBack",
    component: () => import("../page/account/zfbBack"),
    meta: {
      title: "支付宝回调"
    }
  },
  {
    name: "register",
    component: () => import("../page/account/register"),
    meta: {
      title: "注册"
    }
  },
  {
    component: () => import("../page/report/index"),
    name: "report",
    meta: {
      title: "运营报表"
    }
  },
  {
    component: () => import("../page/user/index"),
    name: "user",
    meta: {
      title: "我的"
    }
  },
  {
    path: "/user/info",
    component: () => import("../page/user/info/detail"),
    name: "user",
    meta: {
      title: "账号管理"
    }
  },
  {
    name: "userList",
    component: () => import("../page/user/order/userlist"),
    meta: {
      title: "客户列表"
    }
  },
  {
    name: "tabledetail",
    component: () => import("../page/report/detail"),
    meta: {
      title: "报表明细"
    }
  },
  {
    name: "userOrderDetail",
    component: () => import("../page/user/order/orderdetail"),
    meta: {
      title: "客户订单"
    }
  },
  {
    name: "addressList",
    component: () => import("../page/user/address/list"),
    meta: {
      title: "我的地址"
    }
  },
  {
    name: "addressEdit",
    component: () => import("../page/user/address/edit"),
    meta: {
      title: "修改地址"
    }
  },
  {
    name: "editPassword",
    component: () => import("../page/user/edit/password"),
    meta: {
      title: "修改密码"
    }
  },
  {
    name: "bill",
    component: () => import("../page/user/bill/index"),
    meta: {
      title: "我的账单"
    }
  },
  {
    path: "/user/favorite",
    component: () => import("../page/user/favorite/list"),
    meta: {
      title: "我的收藏"
    }
  },
  {
    path: "/user/coupon",
    component: () => import("../page/user/coupon/list"),
    meta: {
      title: "我的优惠券"
    }
  },
  {
    name: "userOrder",
    component: () => import("../page/user/order/list"),
    meta: {
      title: "我的订单"
    }
  },
  {
    name: "logistics",
    component: () => import("../page/user/order/logistics"),
    meta: {
      title: "订单追踪"
    }
  },
  {
    path: "/user/aftersale",
    component: () => import("../page/user/aftersale/list"),
    meta: {
      title: "售后"
    }
  },
  {
    path: "/user/aftersale/apply",
    component: () => import("../page/user/aftersale/apply"),
    meta: {
      title: "申请售后"
    }
  },
  {
    name: "aftersaleDetail",
    component: () => import("../page/user/aftersale/detail"),
    meta: {
      title: "服务单详情"
    }
  },
  {
    path: "/user/aftersale/track/:id",
    component: () => import("../page/user/aftersale/track"),
    meta: {
      title: "进度详情"
    }
  },
  {
    name: "productDetail",
    component: () => import("../page/product/detail"),
    meta: {
      title: "商品详情"
    }
  },
  {
    path: "/search",
    component: () => import("../page/product/list"),
    meta: {
      title: "商品列表"
    }
  },
  {
    name: "cart",
    component: () => import("../page/cart/index"),
    meta: {
      title: "购物车"
    }
  },
  {
    name: "order",
    component: () => import("../page/shipping/order"),
    meta: {
      title: "确认订单",
      keepAlive: true
    }
  },
  {
    name: "category",
    component: () => import("../page/category/index"),
    meta: {
      title: "分类"
    }
  },
  {
    name: "payment",
    component: () => import("../page/payment"),
    meta: {
      title: "支付方式"
    }
  },
  {
    name: "Alipay",
    component: () => import("../page/Alipay"),
    meta: {
      title: "支付宝支付"
    }
  },
];

// add route path
routes.forEach(route => {
  route.path = route.path || "/" + (route.name || "");
});

const router = new Router({ routes });

router.beforeEach((to, from, next) => {
  const title = to.meta && to.meta.title;
  if (title) {
    document.title = title;
  }
  if (to.path === "/login") {
    sessionStorage.setItem("jumpurl", from.fullPath)
  }
  next();
});

export { router };
